[#import "/framework/JGP.htm"  as JGP]
<!--
项目   parent
作者   LOUFEI [Gavin]
时间   2018/4/13
-->
[@JGP.head "Title"/]
[@JGP.body]
    <jgp-div _col="['300','350']" _cunit="px" _row="['40','*']" _runit="px" _aux="true">
        <div slot="1-1">
            <jgp-btn _fn="addRole">添加角色</jgp-btn>
        </div>
        <jgp-panel slot="2-1" _padding="10px" _border="right">
            <jgp-tree _id="treeRole" _root="角色树" _check="true" _single="true" _icon="false"
                      _onclick="setFormData"
                      _tools="[{'icon':'cubes','tip':'编辑权限','fn':'editRes'},
                      {'icon':'list','tip':'编辑菜单','fn':'editMenuPermission'}]"
                      _url="/security/admin/adminApiController/treeRole"></jgp-tree>
        </jgp-panel>
        <div slot="1-2">
            <jgp-btn _fn="saveCallback" _form="roleForm">保存</jgp-btn>
        </div>
        <jgp-div slot="2-2" _row="['150','*']" _runit="px">
            <jgp-form slot="1-1" _id="roleForm" _action="/security/admin/adminApiController/saveRole" _cols="12"
                      _fdata='${fdata!''}'>
                <jgp-text _name="name" _col="12" _label="角色名" _placeholder="角色名" _required="true"></jgp-text>
                <jgp-text _name="icon" _col="6" _label="图标" _placeholder="图标" _required="true"></jgp-text>
                <jgp-num _name="sort" _col="6" _label="排序" _placeholder="排序" _required="true"></jgp-num>
                <jgp-area _name="remark" _col="12" _label="描述" _placeholder="描述"></jgp-area>
            </jgp-form>
            <jgp-grid slot="2-1" _check="false" _index="false"></jgp-grid>
        </jgp-div>
    </jgp-div>
[/@JGP.body]

[@JGP.foot]
    <script>
        function addRole() {
            var parentTreeNode = JGP.tree('treeRole').getSelected();
            var parentId;
            if (parentTreeNode == -1 || !parentTreeNode) {
                parentId = -1;
                JGP.$confirm('是否添加顶级角色', function () {
                    addRoleWin(parentId);
                });
            } else if (parentTreeNode.length > 0) {
                parentId = parentTreeNode[0].id;
                addRoleWin(parentId);
            }
        }

        function addRoleWin(parentId) {
            var dialog = {
                'title': '添加角色',
                'width': '400px',
                'height': '300px',
                'url': '/security/addRole/' + parentId,
                'callback': function (res) {
                    if (res) {
                        JGP.$alert("保存成功");
                        location.reload();
                    }
                }
            }
            JGP.$dialog(dialog);
        }

        function setFormData(result) {
            if (result.back.bean != -1) {
                JGP.form('roleForm').setData(result.back.bean);
            }
        }

        function saveCallback(result) {
            if (result.back && result.back.flag)
                JGP.$alert("保存成功");
                location.reload();
        }

        function editRes(result){
            var dialog = {
                'title': '编辑权限',
                'width': '700px',
                'height': '600px',
                'url': '/security/chooseRes/'+result.back.id,
                'callback': function (res) {
                    console.log(res);
                }
            }
            JGP.$dialog(dialog);
        }

        function editMenuPermission(result){
            var dialog = {
                'title': '编辑菜单权限',
                'width': '700px',
                'height': '600px',
                'url': '/security/chooseMenus/'+result.back.id,
                'callback': function (res) {
                    console.log(res);
                }
            }
            JGP.$dialog(dialog);

        }
    </script>
[/@JGP.foot]